/**
 * Created by Jerry on 2017/10/27.
 */
import wepy from 'wepy'
import Tips from './Tips';


//wepy.$instance.globalData 获取全局的globalData
// HTTP工具类
export default class http {
  static async request (method, url, data,type) {
    // const header = this.createAuthHeader();
    //header统一在app拦截器中加
    if(!type) Tips.loading();
    const param = {
      url: url,
      method: method,
      // header: header,
      data: data
    }
    const res = await wepy.request(param);
    if(res) Tips.hideLoading()

    console.log(res)


    //统一的容错处理
    if(!res.result){
      return res.data
    }else{
     if(!type) wepy.$instance.globalData.toast.$emit('show',res.msg)
    }


  }


  //如果传入type，则是特殊处理，此时将不弹出loading和toast
  static get (url, data,type) {
    return this.request('GET', url, data,type)
  }

  static post (url, data,type) {
    return this.request('POST', url, data,type)
  }

}
